package com.example.primarycalculator2;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.ArrayList;
import java.util.Random;

//生成问题
public class ProblemsGenerator {
    ArrayList<Problem> problems=new ArrayList<>();
    public static final String TAG="ProblemsGenerator";

    public Problem generateProblem() {
        Random random = new Random();
        int number=random.nextInt(1000);
        int first=number/100;
        int second=number/10-10*first;
        int third=number%10;
        float sum = 0;

//        操作符有4种
        int sign1 = random.nextInt(4);
        int sign2 = random.nextInt(4);
//        括号有3种，左，右
        int sign0 = random.nextInt(2);
//        辅助标记括号是否有用
        int flag0 = 0;
        int flag1 = 0;
        String equation = "";
        String new_equ = "";
//        flag1=1表示括号无用
        if (sign0 == 1) {
//            括号在右
            switch (sign2) {
                case 0:
                    sum += second + third;
                    equation = String.valueOf(second) + "+" + String.valueOf(third);
                    break;
                case 1:
                    sum += second - third;
                    equation = String.valueOf(second) + "-" + String.valueOf(third);
                    break;
                case 2:
                    sum += second * third;
                    flag0 = 1;
                    equation = String.valueOf(second) + "*" + String.valueOf(third);
                    break;
                case 3:
                    if (third == 0) {
//                        重来
                        return generateProblem();
                    }
                    sum += (float) second / third;
                    flag0 = 1;
                    equation = String.valueOf(second) + "÷" + String.valueOf(third);
                    break;

            }
            switch (sign1) {
                case 0:
                    sum += first;
                    if (flag0 == 1) {
                        new_equ = first + "+" + equation + "= " ;
                    } else {
                        new_equ = first + "+(" + equation + ")" + "= " ;
                    }
                    break;
                case 1:
                    sum = first - sum;
                    if (flag0 == 1) {
                        flag1 = 1;
                        new_equ = first + "-" + equation + "= " ;
                    } else {
                        new_equ = first + "-(" + equation + ")" + "= " ;
                    }
                    break;
                case 2:
                    sum *= first;
                    new_equ = first + "*(" + equation + ")" + "= " ;

                    break;
                case 3:
                    if (sum == 0) {
//                        重来
                        return generateProblem();
                    }
                    sum = (float) first / sum;
                    new_equ = first + "÷(" + equation + ")" + "= " ;
                    break;
            }
            if ((int)sum==sum){
                return new Problem(new_equ,"",String.valueOf((int)sum));
            }else {
                return generateProblem();
            }

        } else {
//            括号在左
            switch (sign1) {
                case 0:
                    sum += second + first;
                    equation = String.valueOf(first) + "+" + String.valueOf(second);
                    flag0 = 1;
                    break;
                case 1:
                    sum += first - second;
                    equation = String.valueOf(first) + "-" + String.valueOf(second);
                    flag0 = 1;
                    break;
                case 2:
                    sum += second * first;
                    equation = String.valueOf(first) + "×" + String.valueOf(second);
                    break;
                case 3:
                    if (second == 0) {
//                        重来
                    }
                    sum += (float) first / second;
                    equation = String.valueOf(first) + "÷" + String.valueOf(second);
                    break;

            }
            switch (sign2) {
                case 0:
                    sum += third;
                    new_equ = equation + "+" + third + "= ";
                    break;
                case 1:
                    sum -= third;
                    new_equ = equation + "-" + third + "= ";
                    break;
                case 2:
                    sum *= third;
                    if (flag0 == 1) {
                        new_equ = "(" + equation + ")*" + third + "= ";
                    } else {
                        new_equ = equation + "*" + third + "= " ;
                    }
                    break;
                case 3:
                    if (third == 0) {
//                        重来
                    }
                    sum = (float) sum / third;
                    if (flag0 == 1) {
                        new_equ = "(" + equation + ")÷" + third + "= " ;
                    } else {
                        new_equ = equation + "÷" + third + "= ";
                    }
                    break;
            }
            if ((int)sum==sum){
                return new Problem(new_equ,"",String.valueOf((int)sum));
            }else {

                return generateProblem();
            }



        }



    }


    public ArrayList<Problem> create(int num){
        while (num>0){
            Problem po=generateProblem();
            num--;
            problems.add(po);
        }

        return problems;
    }

    public ArrayList<Problem> checkAnswer(ArrayList<Problem> pros){
        int num=pros.size();
        num-=1;

        while (num>=0){
            Problem pro=pros.get(num);
            if(pro.getAnswer().equals("")){
                pros.get(num).setPicId(R.drawable.question);
//                保存到数据库
                pros.get(num).save();
                Log.d(TAG, "checkAnswer: 保存数据啦");
                num--;
                continue;
            }
            float answer=Float.valueOf(pro.getAnswer().trim());
            float real_answer=Float.valueOf(pro.getRealAnswer().trim());
            if (answer==real_answer){
                pros.get(num).setPicId(R.drawable.right);
            }else {
                pros.get(num).setPicId(R.drawable.false1);
//                保存到数据库
                pros.get(num).save();
                Log.d(TAG, "checkAnswer: 保存数据啦");
            }
            num--;
        }
        return pros;

    }


}
